package hasalah.demo.client.views;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.PopupPanel;

public class PopupDefaultPanel extends Composite {

	private final DecoratorPanel decoratorPanel;
	private final String title;
	private PopupPanel  panel;
	
	public PopupDefaultPanel(String title) {

		this.title = title;
		
		decoratorPanel = new DecoratorPanel();

	}
	

	public void setPopupPanel(PopupPanel panel) {
		this.panel = panel;
	}

	public void setBody(IsWidget widget) {

		decoratorPanel.setWidget(widget);
		
	}

	public IsWidget getMe() {

		FlexTable flexTable = new FlexTable();
		
		flexTable.addStyleName("cw-FlexTable");
		flexTable.setWidth("500px");
		flexTable.setCellSpacing(5);
		flexTable.setCellPadding(3);
				
		// Adding a title
		flexTable.setWidget(1, 1, new HTML(this.title));
		
		flexTable.getCellFormatter().setAlignment(1, 1, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_MIDDLE);
		
		// Adding the panel
		flexTable.setWidget(2, 1, this.decoratorPanel);
		
		// Adding the buttons
		FlexTable buttonTable = new FlexTable();
	
		buttonTable.setCellSpacing(5);
		buttonTable.setCellPadding(3);
		buttonTable.setWidth("500px");
		
		buttonTable.addStyleName("cw-FlexTable");
		buttonTable.setCellSpacing(5);
		buttonTable.setCellPadding(3);

		Button saveButton = new Button("Save");
		buttonTable.setWidget(1, 1, saveButton);
		
		buttonTable.setWidget(1 ,3 ,null);
		
		saveButton.addClickHandler(new ClickHandler() {
			
			public void onClick(ClickEvent event) {
				
				// TODO Save
			}
				
		});
	
		Button closeButton = new Button("Close");
		buttonTable.setWidget(1, 3, closeButton);
		
		closeButton.addClickHandler(new ClickHandler() {
			
			public void onClick(ClickEvent event) {
				
				panel.hide();
			}
				
		});
	
		buttonTable.getCellFormatter().setAlignment(1, 1, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);
		buttonTable.getCellFormatter().setAlignment(1, 2, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_MIDDLE);
		
		flexTable.setWidget(3,1, buttonTable);
		
		
		return flexTable;
	}
	
	

}
